package com.aisino.controller;

import com.aisino.entity.*;
import com.aisino.mapper.*;
import com.aisino.service.HeBingService;
import com.aisino.util.HandleDataUtil;
import com.aisino.util.MapAndObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/cgzb")
@Controller
public class HandleDataController {
	@Resource
	HeBingService heBinService;
	@Resource
	HeBingMapper heBingMapper;
	@Resource
	SelectOrderMapper selectOrderMapper;
	@Resource
	TSysorderheadMapper tSysorderheadMapper;
	@Resource
	TSysorderlineMapper tSysorderlineMapper;
	@Resource
	TOrderDataPcDjMapper tOrderDataPcDjMapper;

	
	@RequestMapping("/hebing")
	@ResponseBody
	public Object hebing(HttpServletRequest request,Model model){
		return heBinService.heBingInsertData();
	}
	
	@RequestMapping("/chaifen")
	@ResponseBody
	public Object chaifen(HttpServletRequest request,Model model){
		return heBinService.chaifen();
	}
	// 获取开具成功发票头数据
	@RequestMapping("/getOrderHeadList")
	@ResponseBody
	public Object getOrderHeadList(@RequestBody Map<String,Object>map){
		System.out.println("接受参数"+map);
		Map<String,String> mapSearch=new HashMap<>();
		String searchMap=(String)map.get("dataStr");
//		String searchMap="{'PCH':'2018/12/01 ~ 2018/12/11','DJHM':'','GFMC':'','weiKaiDate':''}";
//		String searchMap=null;
		if(searchMap!=null&&!"".equals(searchMap)){
			JSONObject  jasonObject = JSON.parseObject(searchMap);
			mapSearch = (Map)jasonObject;
			
			// 获取开票开始结束日期
			String timeString = mapSearch.get("yiKaiDate");
			if(timeString!=null&&!"".equals(timeString)){
				HandleDataUtil.handleLayuiDate(mapSearch,timeString,0);
			}
			// 获取获取数据的开始结束日期
			String getDataTime = mapSearch.get("weiKaiDate");
			if(getDataTime!=null&&!"".equals(getDataTime)){
				HandleDataUtil.handleLayuiDate(mapSearch,getDataTime,1);
			}
		}
		// 如果要查询未开具的
		if(((String) map.get("isKaiPiao")).equals("0")){
			mapSearch.put("isKaiPiao","123");
		}
		
		PageHelper.startPage((Integer)map.get("page"),(Integer)map.get("limit"));

		System.out.println(mapSearch);
		Page<Map<String, String>>headListMaps=selectOrderMapper.selectOrderHead(mapSearch);
		System.err.println(mapSearch.toString()+"**"+headListMaps.size());
		LayUIJson result=new LayUIJson("0","查询成功",(long)headListMaps.getTotal(),headListMaps);
		return result;
	}
	
	

	
	@RequestMapping("/getOrderLineList")
	@ResponseBody
	public Object getOrderLineList(@RequestBody Map<String,Object>map){
		
		PageHelper.startPage((Integer)map.get("page"),1000000);
		Page<Map<String, String>>lineListMaps=selectOrderMapper.selectOrderLine(map);
		
		LayUIJson result=new LayUIJson("0","查询成功",(long)lineListMaps.getTotal(),lineListMaps);
		return result;
	}
	
	// 更新orderdata
	@RequestMapping("/updateOrderData")
	@ResponseBody
	public Object updateOrderData(@RequestBody Map<String,Object>map){
//		TSysorderhead tSysorderhead=tSysorderheadMapper.selectByPrimaryKey("20181023140819275_7Iz4i_cf_001");
//		JSONObject jsonObject=JSON.toJSONString(tSysorderhead);
//		System.out.println(tSysorderhead);
		
		TSysorderline tSysorderline=new TSysorderline();
		// 赋值
		MapAndObjectUtil.copyPropertiesInclude(map,tSysorderline);
		System.out.println(tSysorderline);
		
		return tSysorderline;
	}


	// 作废恢复
	@RequestMapping("/DjRemoveCancel")
	@ResponseBody
	public Object DjRemoveCancel(@RequestBody List<Map<String,String>> list) {
		for (int i = 0; i < list.size(); i++) {
			TOrderDataPcDj tOrderDataPcDj=new TOrderDataPcDj();
			tOrderDataPcDj.setDJHM(list.get(i).get("DJHM"));
			tOrderDataPcDj.setZFBS("0");
			tOrderDataPcDjMapper.updateByPrimaryKeySelective(tOrderDataPcDj);
		}
		Json json=new Json(true,0,"取消作废成功!");
		return json;
	}

	// 作废单据
	@RequestMapping("/DjCancel")
	@ResponseBody
	public Object DjCancel(@RequestBody List<Map<String,String>> list) {
		// 先校验
		for (int i = 0; i < list.size(); i++) {
			String DJHM=list.get(i).get("DJHM");
			TOrderDataPcDj tOrderDataPcDj=tOrderDataPcDjMapper.selectByPrimaryKey(DJHM);

			if("1".equals(tOrderDataPcDj.getHBBS())){
				new Json(false,1,"该单据号码："+DJHM+"已合并，不能作废！");
			}
			if("1".equals(tOrderDataPcDj.getYKFPBS())){
				new Json(false,1,"该单据号码："+DJHM+"已开票，不能作废！");
			}
		}

		for (int i = 0; i < list.size(); i++) {
			String DJHM=list.get(i).get("DJHM");
			TOrderDataPcDj tOrderDataPcDj = new TOrderDataPcDj();
			tOrderDataPcDj.setDJHM(DJHM);
			tOrderDataPcDj.setZFBS("1");
			tOrderDataPcDjMapper.updateByPrimaryKeySelective(tOrderDataPcDj);
			// 删除该单据的所有数据
			List<String>listTemp = selectOrderMapper.selectOhid(DJHM);
			for (int j = 0; j < listTemp.size(); j++) {
				tSysorderheadMapper.deleteByPrimaryKey(listTemp.get(j));
				tSysorderlineMapper.deleteByPrimaryKey(listTemp.get(j));
			}
		}
		Json result=new Json(true,0,"作废单据成功!");
		return result;
	}

}
